package online.misscl.lsy.service.impl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import online.misscl.lsy.entity.*;
import online.misscl.lsy.entity.dto.GetVaccineInfoDTO;
import online.misscl.lsy.entity.dto.vo.VaccineInformationVo;
import online.misscl.lsy.mapper.VaccineInformationMapper;
import online.misscl.lsy.service.VaccineInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 疫苗信息表 服务实现类
 * </p>
 *
 * @author misscl
 * @since 2023/03/11 11:14
 */
@Service
public class VaccineInformationServiceImpl extends ServiceImpl<VaccineInformationMapper, VaccineInformation> implements VaccineInformationService {

    @Override
    public List<VaccineInformationVo> getVaccineInfo(GetVaccineInfoDTO dto) {
        MPJLambdaWrapper<VaccineInformation> vaccineInformationMPJLambdaWrapper = new MPJLambdaWrapper<>();
        vaccineInformationMPJLambdaWrapper.select(VaccineInformation::getVaccineId,VaccineInformation::getVaccineName,VaccineInformation::getVaccinePrice,
                VaccineInformation::getVaccineFactory,VaccineInformation::getVaccineType,VaccineInformation::getVaccineIntro,VaccineInformation::getVaccineImg,
                VaccineInformation::getSuitableAge)
                .leftJoin(VaccineSortHospital.class,VaccineSortHospital::getId, VaccineInformation::getHopitalSortId)
                .leftJoin(VaccineSort.class,VaccineSort::getVaccineSortId,VaccineSortHospital::getVaccineSortId)
                .leftJoin(VaccineNum.class,VaccineNum::getVaccineId,VaccineInformation::getVaccineId)
                .select(VaccineNum::getVaccineNum)
                .eq(VaccineSortHospital::getHospitalId,dto.getHospitalId())
                .eq(dto.getVaccineId() != null,VaccineInformation::getVaccineId,dto.getVaccineId())
                .eq(dto.getSortId() != null,VaccineSort::getVaccineSortId,dto.getSortId());
        return baseMapper.selectJoinList(VaccineInformationVo.class,vaccineInformationMPJLambdaWrapper);
    }

    @Override
    public VaccineInformationVo getOneByVacNameAndHosId(String vaccineName, Integer hospitalId) {
        //根据疫苗名称判断是否存在
        MPJLambdaWrapper<VaccineInformation> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(VaccineInformation::getVaccineId)
                .leftJoin(VaccineSortHospital.class,VaccineSortHospital::getId,VaccineInformation::getHopitalSortId)
                .eq(VaccineInformation::getVaccineName,vaccineName)
                .eq(VaccineSortHospital::getHospitalId, hospitalId)
        ;

        return baseMapper.selectJoinOne(VaccineInformationVo.class,queryWrapper);
    }
}
